package cc.lj.internal.coupons.interfaces.facade;

import cc.lj.internal.core.exception.SysExceptionEnum;
import cc.lj.internal.core.http.HttpResult;
import cc.lj.internal.coupons.application.service.TicketMainAppService;
import cc.lj.internal.coupons.interfaces.dto.TicketMainDTO;
import cc.lj.internal.coupons.interfaces.dto.command.NullifyByIdsTicketCommand;
import cc.lj.internal.coupons.interfaces.dto.query.TicketMainGetListByIdsQuery;
import cc.lj.internal.coupons.interfaces.dto.convert.TicketMainDTOConvertDO;
import io.swagger.annotations.ApiOperation;
import lj.internal.dict.enums.ticketBatch.EnumTicketStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 电子券表ticket_main对外接口 api
 * @author liangxifeng
 * @date 2023/10/11 14:56
 */
@RestController
@Slf4j
@Validated
@RequestMapping("ticketMain")
public class TicketMainController {
    @Autowired
    private TicketMainAppService ticketMainAppService;

    /**
     * 通过主键批量查询电子券数据
     * @param ticketMainGetListByIdsQuery
     * @return 电子券list
     */
    @PostMapping("/getListByIds")
    @ApiOperation(value = "通过主键批量查询电子券批次数据",notes = "错误码：无")
    public HttpResult<List<TicketMainDTO>> getListByIds(@Validated @RequestBody TicketMainGetListByIdsQuery ticketMainGetListByIdsQuery) {
        List<TicketMainDTO> listDTO = ticketMainAppService.getByIds(ticketMainGetListByIdsQuery.getIdList()).stream()
                .map(ticketMainDO -> TicketMainDTOConvertDO.INSTANCE.convert(ticketMainDO))
                .collect(Collectors.toList());
        return HttpResult.success(SysExceptionEnum.SUCCESS.getCode(),SysExceptionEnum.SUCCESS.getMessage(),listDTO);
    }
    /**
     * 通过主键批量修改 ticket_main 表
     * @param ticketCancelCommand
     * @return 影响行数
     */
    @PostMapping("/nullifyByIds")
    public HttpResult<Integer> nullifyByIds(@Validated @RequestBody NullifyByIdsTicketCommand ticketCancelCommand) {
        TicketMainDTO ticketMainDTO = new TicketMainDTO();
        ticketMainDTO.setTicketStatus(EnumTicketStatus.CANCEL.getCode());
        Integer rows = ticketMainAppService.updateByIds(ticketCancelCommand.getIdList(),ticketMainDTO);
        return HttpResult.success(SysExceptionEnum.SUCCESS.getCode(),SysExceptionEnum.SUCCESS.getMessage(),rows);
    }
}
